package com.wkq.common.glide

import android.content.Context
import android.text.TextUtils
import android.widget.ImageView
import com.bumptech.glide.Priority
import com.bumptech.glide.request.RequestOptions
import com.wkq.common.R
import com.wkq.util.UriUtil


/**
 *
 *@Author: wkq
 *
 *@Time: 2025/4/14 12:09
 *
 *@Desc:  Glide 工具类  错误图 占位图 分区域缓存
 */
class GlideUtil  {
    companion object {
        @Volatile
        private var instance: GlideUtil? = null
        fun getInstance(): GlideUtil {
            if (instance == null) {
                synchronized(GlideUtil::class.java) {
                    if (instance == null) {
                        instance = GlideUtil()
                    }
                }
            }
            return instance!!
        }
    }

    /**
     * 加载图片
     * @param context Context
     * @param url String
     * @param error Int
     * @param placeholder Int
     * @param imageView ImageView
     * @param type String
     */
 fun loadImage(
        context: Context,  res: Any,imageView: ImageView,type:String="", error: Int = R.mipmap.iv_glide_error, placeholder: Int = R.mipmap.iv_default9
    ) {
        val request =  RequestOptions()
        request.error(error)
        request.placeholder(placeholder)
        request.priority(Priority.HIGH)
        loadImageByType(context,res,imageView,request,type)
    }

    fun loadImage(context: Context,  url: Any,imageView: ImageView,type:String="",requestOptions: RequestOptions?=null) {
        loadImageByType(context,url,imageView,requestOptions,type)
    }
    /**
    * 封面
     */
    fun loadCoverImage(context: Context,  res: Any, imageView: ImageView,  error: Int = R.mipmap.iv_glide_error, placeholder: Int = R.mipmap.iv_default9){

        val request =  RequestOptions()
        request.error(error)
        request.placeholder(placeholder)
        request.priority(Priority.HIGH)
        loadImageByType(context,res,imageView,request,GlideConstant.glide_save_dir_cover)
    }

    /**
     * 加载用户头像
     * @param context Context
     * @param url String
     * @param error Int
     * @param placeholder Int
     * @param imageView ImageView
     */
    fun loadUserImage(context: Context,  res: Any, imageView: ImageView,  error: Int = R.mipmap.iv_glide_error, placeholder: Int = R.mipmap.iv_default9){

        val request =  RequestOptions()
        request.error(error)
        request.placeholder(placeholder)
        request.priority(Priority.HIGH)
        loadImageByType(context,res,imageView,request,GlideConstant.glide_save_dir_user)
    }

    fun loadUserImage(context: Context,  res: Any, imageView: ImageView ,requestOptions: RequestOptions?=null){
        loadImageByType(context,res,imageView,requestOptions,GlideConstant.glide_save_dir_user)
    }

    /**
     * 加载礼物
     * @param context Context
     * @param url String
     * @param error Int
     * @param placeholder Int
     * @param imageView ImageView
     */
    fun loadGiftImage(
        context: Context, res: Any, imageView: ImageView, error: Int = R.mipmap.iv_glide_error,
        placeholder: Int = R.mipmap.iv_default9
    ){
        val request =  RequestOptions()
        request.error(error)
        request.placeholder(placeholder)
        request.priority(Priority.HIGH)
        loadImageByType(context,res,imageView,request,GlideConstant.glide_save_dir_gift)
    }

    fun loadGiftImage(context: Context,  res: Any, imageView: ImageView ,requestOptions: RequestOptions?=null){
        loadImageByType(context,res,imageView,requestOptions,GlideConstant.glide_save_dir_gift)
    }

//   /**
//     * 加载等级图片
//     * @param context Context
//     * @param url String
//     * @param error Int
//     * @param placeholder Int
//     * @param imageView ImageView
//     */
//    fun loadLevelImage(
//       context: Context, res: Any, imageView: ImageView, error: Int = R.mipmap.iv_glide_error,
//       placeholder: Int = R.mipmap.iv_default9
//   ){
//        val request =  RequestOptions()
//        request.error(error)
//        request.placeholder(placeholder)
//       request.priority(Priority.HIGH)
//        loadImageByType(context,res,imageView,request,GlideConstant.glide_save_dir_level)
//    }
//
//
//    fun loadLevelImage(context: Context,  res: Any, imageView: ImageView ,requestOptions: RequestOptions?=null){
//        loadImageByType(context,res,imageView,requestOptions,GlideConstant.glide_save_dir_level)
//    }


    /**
     * 处理按类型缓存图片
     * @param context Context
     * @param url String
     * @param imageView ImageView
     * @param requestOptions RequestOptions?
     * @param type String 图片类型 GlideConstant
     */
  private  fun loadImageByType(   context: Context,  url: Any, imageView: ImageView, requestOptions: RequestOptions?,
                           type: String){
        var imgUrl=url
        if (!TextUtils.isEmpty(type)&&url is String){
            imgUrl= UriUtil.appendParameter(url,GlideConstant.glide_save_type_key,type)
        }

        if (requestOptions!=null){
            GlideApp.with(context).load(imgUrl).apply(requestOptions).centerCrop().into(imageView)

        }else{
            val request =  RequestOptions()
            request.error(R.mipmap.iv_glide_error)
            request.placeholder(R.mipmap.iv_default9)
            request.priority(Priority.HIGH)
            GlideApp.with(context).load(imgUrl).apply(request) .centerCrop().into(imageView)
        }

    }


}